package com.example.javaeeblog20240514.demos;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/test")
public class Test {
    @RequestMapping(value = "/hello",method = RequestMethod.POST)
    public String test1(){
        return "hello world!";
    }
    @RequestMapping("/r1")
    public String r1(String name){
        return "name = " + name;
    }
    @RequestMapping("/r2")
    public String r2(String name, Integer age){
        return "name = " + name+", age = " + age;
    }
    @RequestMapping("/r3")
    public String r3(User user){
        return user.toString();
    }

    @RequestMapping("/r4")
    public String r4(@RequestParam(value = "name", required = false) String username, Integer age){
        return "name = " + username+", age = " + age;
    }
    @RequestMapping("/r5")
    public String r5(int[] arr){
        return Arrays.toString(arr);
    }

    @RequestMapping("/r6")
    public String r6(@RequestParam(required = false) List<Integer> lst){
        if(lst != null){
            return lst.toString();
        }
        return "没收到";
    }
    @RequestMapping("/r7")
    public String r7(@RequestBody User user){
        return user.toString();
    }
    @RequestMapping("/r8/{name}")
    public String r8(@PathVariable String name){
        return name;
    }
    @RequestMapping("/r9")
    public String r9(@RequestPart MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        file.transferTo(new File("D:/二次元/"+filename));
        return filename;
    }

    @RequestMapping("/r10")
    public String r10(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if(cookies == null) return "null";
        String res = "";
        for(Cookie x : cookies){
            res = res +  x.getName() + ":" + x.getValue();
        }
        return res;
    }

    @RequestMapping("/r11")
    public String r11(@CookieValue(value = "Cookie_2",required = false) String value) {
        return value;
    }
    @RequestMapping("/r12")
    public String r12(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        return username;
    }
    @RequestMapping("/r13")
    public String r13(HttpSession session) {
        String username = (String)session.getAttribute("username");
        return username;
    }

    @RequestMapping("/r14")
    public String r14(@SessionAttribute("username") String name) {
        return name;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpSession session){
        session.setAttribute("username","zhangsan");
        return "设置成功";
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return userAgent;
    }
}
